package com.xinzhi.service.impl;


import com.xinzhi.mapper.UsersMapper;
import com.xinzhi.pojo.Bankaccount;
import com.xinzhi.pojo.UserIDNumber;
import com.xinzhi.pojo.dto.Username;
import com.xinzhi.service.UsersService;
import com.xinzhi.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class IUsersServiceImpl implements UsersService {
    @Autowired
    private UsersMapper usersMapper;

    @Override
    public Result getUser(String username, String idnumber) {
        UserIDNumber userIDNumber = usersMapper.selectUser(username, idnumber);
        //判断用户名和身份证号码是否存在
        if (userIDNumber == null)
            return Result.error("用户名或身份证号不存在");
        //判断身份证号码是否正确
        if (!isVailIdNumber(idnumber)) {
            return Result.error("身份证号码格式不正确");
        }
        return Result.ok("通过");
    }

    @Override
    public boolean isVailIdNumber(String idnumber) {
        return idnumber.matches("\\d{17}[\\d|Xx]$");
    }

    @Override
    public Bankaccount getUserByCardNumber(String username, String cardnumber) {
        Bankaccount Bankaccount = usersMapper.selectUsernameByID(username, cardnumber);
        // 如果都通过了，返回绑定成功
        return Bankaccount;
    }
    @Override
    public boolean isVailCardNumber(String cardnumber) {
        // 正则表达式匹配银行卡号格式，这里假设银行卡号是以62开头，后面跟14到17位数字
        return cardnumber.matches("^62\\d{14,17}$");
    }
}
